function getDPI() {
  const arrDPI: any[] = []
  const screen: any = window.screen
  if (screen.deviceXDPI) {
    arrDPI[0] = screen.deviceXDPI
    arrDPI[1] = screen.deviceYDPI
  } else {
    const tmpNode: any = document.createElement('DIV')
    tmpNode.style.cssText = 'width:1in;height:1in;position:absolute;left:0px;top:0px;z-index:99;visibility:hidden'
    document.body.appendChild(tmpNode)
    arrDPI[0] = parseInt(tmpNode.offsetWidth)
    arrDPI[1] = parseInt(tmpNode.offsetHeight)
    tmpNode.parentNode.removeChild(tmpNode)
  }
  return arrDPI
}

// px转毫米
export function pxToMm(value) {
  const inch = value / getDPI()[0]
  return inch * 25.4
}

// 毫米转px
export function mmToPx(value) {
  const inch = value / 25.4
  return inch * getDPI()[0]
}

// 1mm对应x px 和 1px 对应 x mm
export function mmpx() {
  const mm = mmToPx(1)
  const px = pxToMm(1)
  return [mm, px]
}

// pt、px互转
export function ptToPx(num, type?) {
  const dpi = getDPI()[0]; // 默认值 96dpi
  // if (window.devicePixelRatio) {
  //   dpi = dpi * window.devicePixelRatio;
  //   // console.log(window.devicePixelRatio)
  // }
  if (type === 'px') {
    return Math.ceil(num * (72 / dpi))
  } else {
    return Math.ceil(num / (72 / dpi))
  }
}


// 毫米转pt
export function mmToPt(value) {
  const px = mmToPx(value)
  return ptToPx(px, 'pt')
}

function setSize(sizeNum) {
  let size
  const mit = 3.125 / 12
  if (sizeNum === '1') {
    // 3.125 mm 约 12px
    size = 1.5875
  } else if (sizeNum === '1.5') {
    // 3.125 mm 约 12px
    size = 2.0833
  } else if (sizeNum === '2') {
    // 4.7625 mm 约 18px
    size = 2.646
  } else if (sizeNum === '3') {
    // 6.35 mm 约 24px
    size = 3.704
  } else {
    size = mit * sizeNum
  }
  return size
}


export function setNewelement(els, fieldList?) {
  els = els.map(item => {
    item.width = mmToPt(item.width * 1.2)
    item.height = mmToPt(item.height * 1.2)
    item.left = mmToPt(item.left)
    item.top = mmToPt(item.top * 1.2)
    item.type = ['word', 'barCode', 'qrCode', 'business'][item.type]

    if (item.size) {
      item.size = mmToPt(setSize(item.size) * 0.6)
    }
    if (item.labels) {
      item.type = 'business'
      item.separator = item.separate
      item.fields = item.labels.map(field => {
        const { fieldCode, fieldId, fieldName, sampleValue, configId } = field
        return {
          businessType: (() => {
            const obj = (fieldList || []).find(f => f.configId === field.configId) || {}
            return obj.businessType

          })(),
          configId,
          fieldCode,
          fieldId,
          fieldName,
          sampleValue,
        }
      })
    }
    return item
  })
  return els
}